package cn.xspace.wnx.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.LogUtil;

import java.util.TreeMap;

/**
 * 字母表
 */
public class QuickAlphabeticBar extends ImageButton {
    private static String TAG = "QuickAlphabeticBar";
    private TextView mDialogText;
    private Handler mHandler;
    private ListView mList;
    private float mHight;
    private String[] letters;/*= new String[]{"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};*/
    private TreeMap<String, Integer> alphaIndexer;

    public QuickAlphabeticBar(Context context) {
        super(context);
    }

    public QuickAlphabeticBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public QuickAlphabeticBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void init(TextView mDialogText) {
        this.mDialogText = mDialogText;//(TextView) ctx.findViewById(RProxy.msm_cont_fast_position);
        this.mDialogText.setVisibility(View.INVISIBLE);
        mHandler = new Handler();
    }

    public void refreshIndex() {
        letters = alphaIndexer.keySet().toArray(new String[alphaIndexer.size()]);
//        LogUtil.d(letters[0]);
        postInvalidate();
    }

    public void setListView(ListView mList) {
        this.mList = mList;
    }

    public void setAlphaIndexer(TreeMap<String, Integer> alphaIndexer) {
        this.alphaIndexer = alphaIndexer;
        refreshIndex();
    }

    public void setHight(float mHight) {
        this.mHight = mHight;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int act = event.getAction();
        float y = event.getY();
        final int oldChoose = choose;
        // 计算手指位置，找到对应的段，让mList移动段开头的位置上
        int selectIndex = (int) (y / (mHight / letters.length));

        if (selectIndex > -1 && selectIndex < letters.length) {// 防止越界
            String key = letters[selectIndex];
            LogUtil.d(TAG, "key:" + key);
            if (alphaIndexer.containsKey(key)) {
                int pos = alphaIndexer.get(key);
                LogUtil.d(TAG, "pos:" + key);
                if (mList.getHeaderViewsCount() > 0) {// 防止ListView有标题栏，本例中没有。
                    this.mList.setSelectionFromTop(pos + mList.getHeaderViewsCount(), 0);
                } else {
                    this.mList.setSelectionFromTop(pos, 0);
                }
            } else if (alphaIndexer.containsKey(key.toLowerCase())) {
                int pos = alphaIndexer.get(key.toLowerCase());
                LogUtil.d(TAG, "pos:" + key);
                if (mList.getHeaderViewsCount() > 0) {// 防止ListView有标题栏，本例中没有。
                    this.mList.setSelectionFromTop(pos + mList.getHeaderViewsCount(), 0);
                } else {
                    this.mList.setSelectionFromTop(pos, 0);
                }
            } else if (key.equals("#")) {
                if (mList.getHeaderViewsCount() > 0) {// 防止ListView有标题栏，本例中没有。
                    this.mList.setSelectionFromTop(0 + mList.getHeaderViewsCount(), 0);
                } else {
                    this.mList.setSelectionFromTop(0, 0);
                }
            }
            mDialogText.setText(letters[selectIndex]);
        }

        switch (act) {
            case MotionEvent.ACTION_DOWN:
                showBkg = true;

                if (oldChoose != selectIndex) {
                    if (selectIndex > 0 && selectIndex < letters.length) {
                        choose = selectIndex;
                        invalidate();
                    }
                }

                if (mHandler != null) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (mDialogText != null && mDialogText.getVisibility() == View.INVISIBLE) {
                                mDialogText.setVisibility(VISIBLE);
                            }
                        }
                    });
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (oldChoose != selectIndex) {
                    if (selectIndex > -1 && selectIndex < letters.length) {
                        choose = selectIndex;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                showBkg = false;
                choose = -1;
                if (mHandler != null) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (mDialogText != null && mDialogText.getVisibility() == View.VISIBLE) {
                                mDialogText.setVisibility(INVISIBLE);
                            }
                        }
                    });
                }
                break;
            default:
                break;
        }

        return super.onTouchEvent(event);
    }

    // TODO:
    Paint paint = new Paint();
    boolean showBkg = false;
    int choose = -1;

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*if (showBkg) {//设置边栏背景色
            canvas.drawColor(Color.parseColor("#b20264"));
		}*/

        if (letters == null||letters.length == 0) {
            return;
        }
        int height = getHeight();
        int width = getWidth();
        int singleHeight = height / letters.length;
        for (int i = 0; i < letters.length; i++) {
            paint.setColor(Color.BLACK);
            paint.setTextSize(25);
            // paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            if (i == choose) {
                paint.setColor(Color.parseColor("#00BFFF"));// 滑动时按下的字母颜色
                paint.setFakeBoldText(true);
            }
            float xPos = width / 2 - paint.measureText(letters[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(letters[i], xPos, yPos, paint);
            paint.reset();
        }

    }

}
